//
//  MLAHomeNewsDetailVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/20.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLAHomeNewsDetailVC.h"
#import "MLANewsModel.h"

@interface MLAHomeNewsDetailVC ()

@end

@implementation MLAHomeNewsDetailVC {
    UIImageView *bannerImgv;
    UILabel *titleLbl;
    UILabel *dateLbl;
    UILabel *detailLbl;
    
    MLANewsModel *model;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"新闻详情"];
    
}

#pragma mark - LoadData
- (void)setModelId:(NSString *)modelId {
    _modelId = modelId;
    NSDictionary *params = @{@"id":_modelId};
    [MLAHttpManage postHostType:MLAHttpHostType_Api api:kNewsDetail params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        self->model = [MLANewsModel mj_objectWithKeyValues:response];
        [self updateData:self->model];
    } failure:^{
        
    }];
}

- (void)updateData:(MLANewsModel *)model {
    [self addSubviewsInView:model];
}

#pragma mark - Subviews
- (void)addSubviewsInView:(MLANewsModel *)model {
    
    CGFloat height = CX_GetXScale(194)+95+40;
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), height);
    
    bannerImgv = [[UIImageView alloc] init];
    bannerImgv.layer.cornerRadius = 5;
    bannerImgv.layer.masksToBounds = YES;
//    bannerImgv.backgroundColor = CX_GetColorRandom();
    [tableHeaderView addSubview:bannerImgv];
    [bannerImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(CX_GetXScale(194));
        make.top.mas_equalTo(20);
    }];
    
    titleLbl = [[UILabel alloc] init];
    titleLbl.font = CX_GetNormalFont(18);
    titleLbl.textColor = CX_GetColorTitle();
//    titleLbl.text = @"uHome是一个租赁房间的平台";
    [tableHeaderView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(self->bannerImgv.mas_bottom).offset(20);
    }];
    
    dateLbl = [[UILabel alloc] init];
    dateLbl.font = CX_GetNormalFont(10);
    dateLbl.textColor = CX_GetColorHex(0x999999, 1);
//    dateLbl.text = @"2019-09-09";
    [tableHeaderView addSubview:dateLbl];
    [dateLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.top.equalTo(self->titleLbl.mas_bottom).offset(10);
    }];
        
    [bannerImgv sd_setImageWithURL:[NSURL URLWithString:model.pic]];
    titleLbl.text = model.subject;
    dateLbl.text = model.ctime;
    
    detailLbl = [[UILabel alloc] init];
    detailLbl.frame = CGRectMake(0, 0, CX_GetScreenWidth()-30, 0);
    detailLbl.font = CX_GetNormalFont(12);
    detailLbl.textColor = CX_GetColorHex(0x6B7178, 1);
    detailLbl.numberOfLines = 0;
    
    detailLbl.text = model.content;
    [detailLbl sizeToFit];
    [tableHeaderView addSubview:detailLbl];
    [detailLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.equalTo(self->dateLbl.mas_bottom).offset(20);
        make.height.mas_equalTo(CGRectGetHeight(self->detailLbl.frame));
    }];
    
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), height+CGRectGetHeight(self->detailLbl.frame));
    
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(CX_GetScreenWidth());
        make.height.mas_equalTo(height+CGRectGetHeight(self->detailLbl.frame));
    }];
    
}


@end
